#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>


#include <string.h>
#include <semaphore.h>
#include "hfile.h"

#define ACC_RIGHT 600
#define FILEPATH "/tmp/mmappedupload.bin"
#define NUMINTS  (1000)
#define FILESIZE (NUMINTS * sizeof(int))

/*
 *This function will send data(dataBuffer) from the client handler to the mmap for uploading a file.
 */

int sendToFsp(char *Buffer)
{
    int fd;
    int bytesWritten = 0;
    char *map;  /* mmapped array of chars(array of chars) */

    printf("inhoud van Buffer: %s\n:", Buffer);
    fd = open(FILEPATH, O_RDWR);
    if (fd == -1)
    {
	perror("Error opening file for reading");
	exit(EXIT_FAILURE);
    }
    
    bytesWritten = write(fd, Buffer, strlen(Buffer));//write to mmf


    /*Debug code !*/
    map = (char*)mmap(0, FILESIZE,  PROT_WRITE, MAP_SHARED, fd, 0);
    if (map == MAP_FAILED)
    {
        close(fd);
        perror("Error mmapping the file");
        exit(EXIT_FAILURE);
    }

    printf("map: %s\n", map);//Debug for whats in the mmap
  
    return 0;
}

